struct ListNode* reverse(struct ListNode* head)
{
    struct ListNode* n1 = NULL;
    struct ListNode* n2 = head;
    struct ListNode* n3 = NULL;
    if (n2)
        n3 = n2->next;

    while (n2)
    {
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if (n3)
            n3 = n3->next;
    }

    return n1;
}

struct ListNode* reverseBetween(struct ListNode* head, int left, int right)
{

    if (head == NULL || left >= right)
    {
        return head;
    }

    struct ListNode* phead = malloc(sizeof(struct ListNode));
    phead->next = head;

    struct ListNode* prev = NULL;
    struct ListNode* cur1 = phead;
    struct ListNode* cur2 = phead;
    struct ListNode* Back = NULL;
    struct ListNode* next = NULL;

    int num1 = left;
    int num2 = right;
    while (num1--)
    {
        prev = cur1;
        cur1 = cur1->next;
    }

    while (num2--)
    {
        cur2 = cur2->next;
    }

    next = cur2->next;
    cur2->next = NULL;

    Back = reverse(cur1);
    prev->next = Back;

    int num = right - left;
    while (num--)
    {
        Back = Back->next;
    }
    if (Back)
        Back->next = next;

    head = phead->next;
    free(phead);

    return head;
}